/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.execute.service.impl;

import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.constant.ExecuteConstant;
import com.icinfo.cloud.provider.common.enums.FlowStageTypeEnum;
import com.icinfo.cloud.provider.punish.csource.manage.dto.FlowCommonDto;
import com.icinfo.cloud.provider.punish.csource.manage.dto.FlowRecordDto;
import com.icinfo.cloud.provider.punish.csource.manage.dto.FlowRecordVo;
import com.icinfo.cloud.provider.punish.csource.manage.model.AppointedPersonInfo;
import com.icinfo.cloud.provider.punish.csource.manage.model.CaseSourceFlowRecord;
import com.icinfo.cloud.provider.punish.execute.dto.ExecuteFlowCommonDto;
import com.icinfo.cloud.provider.punish.execute.dto.ExecuteFlowRecordRequest;
import com.icinfo.cloud.provider.punish.execute.dto.ExecuteFlowRecordVo;
import com.icinfo.cloud.provider.punish.execute.enums.ExecuteStateEnum;
import com.icinfo.cloud.provider.punish.execute.mapper.ExecuteFlowRecordMapper;
import com.icinfo.cloud.provider.punish.execute.model.ExecuteFlowRecord;
import com.icinfo.cloud.provider.punish.execute.service.IExecuteFlowRecordService;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 描述: 裁执分离登记流程记录表 execute_flow_record 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年08月26日
 */
@Service
public class ExecuteFlowRecordServiceImpl extends MyBatisServiceSupport implements IExecuteFlowRecordService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ExecuteFlowRecordServiceImpl.class);

    @Autowired
    private ExecuteFlowRecordMapper executeFlowRecordMapper;

    /**
     * 查询流转日志
     * @author liyafeng
     * @date 2022/8/15
     * @param flowRecordDto
     * @return {@link List < FlowRecordVo>}
     */
    @Override
    public List<ExecuteFlowRecordVo> getFlowRecordVoList(ExecuteFlowRecordRequest flowRecordDto) {
        List<ExecuteFlowRecordVo> flowRecordVos=executeFlowRecordMapper.selectFlowRecordList(flowRecordDto);
        return flowRecordVos;
    }
    /**
     * 查询流转日志
     * @author liyafeng
     * @date 2022/8/15
     * @param flowRecordDto
     * @return {@link List < FlowRecordVo>}
     */
    @Override
    public List<ExecuteFlowRecord> getFlowRecordList(ExecuteFlowRecordRequest flowRecordDto) {
        Example example = new Example(ExecuteFlowRecord.class);
        example.createCriteria().andEqualTo("executeId",flowRecordDto.getExecuteId());
        example.setOrderByClause("flow_end_time asc");
        List<ExecuteFlowRecord> flowRecords=executeFlowRecordMapper.selectByExample(example);
        return flowRecords;
    }
    /**
     *通用日志保存
     * @author liyafeng
     * @date 2022/8/15
     * @param flowCommonDto
     * @return {@link int}
     */
    @Override
    public int saveCommonFlowRecord(ExecuteFlowCommonDto flowCommonDto) {
        ExecuteFlowRecord flowRecord=new ExecuteFlowRecord();
        flowRecord.setExecuteId(flowCommonDto.getExecuteId());
        flowRecord.setId(StringUtils.uuid());
        flowRecord.setFlowEndTime(flowCommonDto.getFlowEndTime());
        flowRecord.setFlowStartTime(flowCommonDto.getFlowStartTime());
        flowRecord.setFlowStageType(flowCommonDto.getFlowStageType());
        flowRecord.setFlowStageName(FlowStageTypeEnum.getValue(flowCommonDto.getFlowStageType()));
        StringBuffer flowStageContent = new StringBuffer("");
        //裁执分离-流程阶段类型;SY 受案，TZ 通知，GG 公告，QZZX 强制执行，WPCZ 物品处置，JA 结案  通用拼接 flow_stage_content
        List<String> stageTypeList = new ArrayList<String>(Arrays.asList(ExecuteConstant.EXECUTE_STAGE_SA,
                ExecuteConstant.EXECUTE_STAGE_TZ,
                ExecuteConstant.EXECUTE_STAGE_GG,
                ExecuteConstant.EXECUTE_STAGE_QZZX,
                ExecuteConstant.EXECUTE_STAGE_WPCZ,
                ExecuteConstant.EXECUTE_STAGE_JA));
        if(stageTypeList.contains(flowCommonDto.getFlowStageType())){
            if(ExecuteConstant.EXECUTE_STAGE_SA.equals(flowCommonDto.getFlowStageType())||flowCommonDto.getFlowStageType().equals(ExecuteConstant.EXECUTE_STAGE_JA)){
                flowStageContent.append("提请人:");
            }
            if(Constant.CASE_SOURCE_STAGE_LASPTG.equals(flowCommonDto.getFlowStageType())||flowCommonDto.getFlowStageType().equals(Constant.CASE_SOURCE_STAGE_BYLASPTG)){
                flowStageContent.append("审批人:");
            }
            flowStageContent.append(flowCommonDto.getStartPersonName()+"("+flowCommonDto.getStartPersonOrgName()+"/"+flowCommonDto.getStartPersonDeptName()+")");
        }
        flowRecord.setFlowStageContent(flowStageContent.toString());
        int num=executeFlowRecordMapper.insert(flowRecord);
        return num;
    }
    /**
     *查询最新的流转状态
     * @author liyafeng
     * @date 2022/8/15
     * @param
     * @return {@link CaseSourceFlowRecord}
     */
    @Override
    public ExecuteFlowRecord getLastExecuteFlowRecord(String executeId) {
        Example example =new Example(ExecuteFlowRecord.class);
        example.createCriteria().andEqualTo("executeId",executeId);
        example.setOrderByClause("flow_end_time desc");
        List<ExecuteFlowRecord> flowRecords=executeFlowRecordMapper.selectByExample(example);
        if(null!=flowRecords&&flowRecords.size()>0){
            ExecuteFlowRecord flowRecord=flowRecords.get(0);
            return flowRecord;
        }
        return null;
    }

    @Override
    public void dealCurrentAndNextExecuteFlowRecord(String executeId, String currentExecuteState, String nextExecuteState) {
        Example example = new Example(ExecuteFlowRecord.class);
        example.createCriteria().andEqualTo("executeId",executeId).
                andEqualTo("flowStageCode",currentExecuteState);
        List<ExecuteFlowRecord> executeFlowRecords = executeFlowRecordMapper.selectByExample(example);
        if(executeFlowRecords != null && executeFlowRecords.size()>0){
            ExecuteFlowRecord executeFlowRecord= executeFlowRecords.get(0);
            executeFlowRecord.setFlowEndTime(new Date());
            executeFlowRecordMapper.updateByExample(executeFlowRecord,example);
        }else {
            //其他阶段直接跳转到  - 结案阶段
            ExecuteFlowRecord newCurrentFlowRecord = new ExecuteFlowRecord();
            newCurrentFlowRecord.setId(StringUtils.uuid());
            newCurrentFlowRecord.setExecuteId(executeId);
            //开始时间取上次流转时间
            Example lastexample = new Example(ExecuteFlowRecord.class);
            lastexample.createCriteria().andEqualTo("executeId",executeId);
            lastexample.setOrderByClause("flow_start_time desc");
            List<ExecuteFlowRecord> lastFlowRecords = executeFlowRecordMapper.selectByExample(example);
            if(lastFlowRecords != null && lastFlowRecords.size()>0){
                ExecuteFlowRecord lastFlowRecord = lastFlowRecords.get(0);
                if(lastFlowRecord.getFlowEndTime() != null){
                    newCurrentFlowRecord.setFlowStartTime(lastFlowRecord.getFlowEndTime());
                }else{
                    newCurrentFlowRecord.setFlowStartTime(lastFlowRecord.getFlowStartTime());
                }
            }else{
                newCurrentFlowRecord.setFlowStartTime(new Date());

            }
            newCurrentFlowRecord.setFlowEndTime(new Date());
            newCurrentFlowRecord.setFlowStageCode(currentExecuteState);
            newCurrentFlowRecord.setFlowStageType(currentExecuteState);
            newCurrentFlowRecord.setFlowStageName(ExecuteStateEnum.getValueByCode(currentExecuteState));
            newCurrentFlowRecord.setFlowStageContent(ExecuteStateEnum.getValueByCode(currentExecuteState));
            executeFlowRecordMapper.insert(newCurrentFlowRecord);
        }
        if(StringUtils.isNotBlank(nextExecuteState)){
            ExecuteFlowRecord nextFlowRecord = new ExecuteFlowRecord();
            nextFlowRecord.setId(StringUtils.uuid());
            nextFlowRecord.setExecuteId(executeId);
            nextFlowRecord.setFlowStartTime(new Date());
            nextFlowRecord.setFlowStageCode(nextExecuteState);
            nextFlowRecord.setFlowStageType(nextExecuteState);
            nextFlowRecord.setFlowStageName(ExecuteStateEnum.getValueByCode(nextExecuteState));
            nextFlowRecord.setFlowStageContent(ExecuteStateEnum.getValueByCode(nextExecuteState));
            executeFlowRecordMapper.insert(nextFlowRecord);
        }

    }
}